package com.qf.air.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.air.entity.AirQualityIndex;
import com.qf.air.mapper.AirQualityIndexMapper;
import com.qf.air.service.AirQualityIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Administrator
 * @version V1.0
 * @Project AriQualityIndex
 * @Package com.qf.air.service.impl
 * @Description: ${TODO}
 * @Date 2021/9/30 14:51
 */

@Service
public class AirQualityIndexServiceImpl implements AirQualityIndexService {

    @Autowired
    private AirQualityIndexMapper airQualityIndexMapper;

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void insert(AirQualityIndex airQualityIndex) {
        int i = airQualityIndexMapper.insertSelective(airQualityIndex);
        if (i != 1){
            System.out.println("添加空气质量信息失败");
            throw new RuntimeException("添加空气质量信息失败");
        }
    }

    @Override
    @Transactional
    public void deleteById(Integer id) {
        int i = airQualityIndexMapper.deleteByPrimaryKey(id);
        if (i != 1){
            System.out.println("删除空气质量信息失败");
            throw new RuntimeException("删除空气质量信息失败");
        }
    }

    @Override
    public void updateById(AirQualityIndex airQualityIndex) {
        int i = airQualityIndexMapper.updateByPrimaryKeySelective(airQualityIndex);
        if (i != 1){
            System.out.println("修改空气质量信息失败");
            throw new RuntimeException("修改空气质量信息失败");
        }
    }

    @Override
    public AirQualityIndex findById(Integer id) {

        return airQualityIndexMapper.findById(id);
    }

    @Override
    public PageInfo<AirQualityIndex> findByDistrictIdAndPage(Integer page, Integer rows, Integer districtId) {
        //1.分页
        PageHelper.startPage(page, rows);
        //2.查询
        List<AirQualityIndex> airList = airQualityIndexMapper.findByDistrictId(districtId);

        //封装PageInfo
        PageInfo<AirQualityIndex> pageInfo = new PageInfo<>(airList);
        return pageInfo;
    }
}
